package com.quasar.hpatient.view.personal.historyRecord;

import android.content.DialogInterface;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.event.UpdateEvent;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.CaseinformationData;
import com.quasar.hpatient.onInterface.OnCaseHistoryListener;
import com.quasar.hpatient.presenter.CaseHistoryPresenter;
import com.quasar.hpatient.utils.DialogHelp;
import com.quasar.hpatient.view.adapter.CaseAdapter;
import com.quasar.hpatient.widget.dialog_my_date_code;
import com.rey.material.app.Dialog;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 我的病例
 */
@OptionsMenu(R.menu.menu_case)
@EActivity(R.layout.activity_my_case)
public class MyCaseActivity extends BaseActivity implements OnCaseHistoryListener,dialog_my_date_code.OnCustomDialogDetermineListener  {

    @ViewById(R.id.main_toolbar)
    Toolbar main_toolbar;
    @ViewById(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;
    @ViewById(R.id.imageView4)
    TextView imageView4;
    @ViewById(R.id.por_tv_title)
    TextView por_tv_title;

    private CaseHistoryPresenter caseHistoryPresenter;
    private CaseAdapter caseAdapter;
    //记录时间
    String DateTime;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    public static final int ACTION_TYPE_LAST = 5;

    @AfterViews
    void init(){
        initView();
        initData();
    }


    @Override
    protected void initView() {
        por_tv_title.setText("按时间段查询");
        imageView4.setText("一周");
        initToolbar();
        rv_list.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        initAdapter();
        caseHistoryPresenter=new CaseHistoryPresenter(this);
        caseHistoryPresenter.GetPatientCaseHistory(setStatetime(ConstantManger.Aweek), format.format(new Date()));
    }

    //查询时间
    @Click(R.id.ll_rl_title)
    public void rl_title() {
        DialogHelp.getSelectDialog(this, null, getResources().getStringArray(R.array.choose_date), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                goToSelectPicture(i);
            }
        }).show();
    }

    public void initAdapter() {
        caseAdapter=new CaseAdapter();
        rv_list.setAdapter(caseAdapter);
        //修改
        caseAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                switch (view.getId()){
                    case R.id.ll_case:
                        AddCaseActivity_.intent(MyCaseActivity.this).caseinformationData(caseAdapter.getData().get(position)).start();
                        break;
                }
            }
        });
        //删除
        caseAdapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
            @Override
            public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
                deleteCase(caseAdapter.getData().get(position));
                return true;
            }
        });
    }

    //弹出提示删除病例信息
    public void deleteCase(final CaseinformationData caseinformationData){

        Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                super.onPositiveActionClicked(fragment);

            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
                caseHistoryPresenter.DeletePatientCase(caseinformationData.getId()+"");
            }
        };
        builder.title("是否删除该病例信息?")
                .positiveAction("否")
                .negativeAction("是");
        ((SimpleDialog.Builder) builder).message("");
        DialogFragment fragment = DialogFragment.newInstance(builder);

        if (this instanceof AppCompatActivity) {
            AppCompatActivity activity = this;
            fragment.show(activity.getSupportFragmentManager(), null);
        }

    }


    private void goToSelectPicture(int position) {

        String[] Strnum = getResources().getStringArray(R.array.choose_date);

        imageView4.setText(Strnum[position]);

        switch (position) {
            //一周
            case 0:

                DateTime = setStatetime(ConstantManger.Aweek);
                caseHistoryPresenter.GetPatientCaseHistory(DateTime, format.format(new Date()));

                break;

            case 1:
                DateTime = setStatetime(ConstantManger.Twoweeks);
                caseHistoryPresenter.GetPatientCaseHistory(DateTime, format.format(new Date()));

                break;


            case 2:
                DateTime = setStatetime(ConstantManger.January);
                caseHistoryPresenter.GetPatientCaseHistory(DateTime, format.format(new Date()));
                break;


            case 3:
                DateTime = setStatetime(ConstantManger.March);
                caseHistoryPresenter.GetPatientCaseHistory(DateTime, format.format(new Date()));
                break;

            case 4:
                DateTime = setStatetime(ConstantManger.Halfayear);
                caseHistoryPresenter.GetPatientCaseHistory(DateTime, format.format(new Date()));
                break;

            case ACTION_TYPE_LAST:
                dialog_my_date_code dialog = new dialog_my_date_code(this);
                dialog.setListener(this);
                dialog.show();
                break;

        }
    }

    /**
     * 根据传入的参数计算出前多少天的数据
     *
     * @param DataTime
     * @return
     */
    public String setStatetime(int DataTime) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, DataTime);
        Date monday = c.getTime();
        String preMonday = format.format(monday);
        return preMonday;
    }

    //添加病例
    @OptionsItem(R.id.action_add_case)
    public void action_submit() {
        AddCaseActivity_.intent(this).start();
    }

    @Override
    public void onListSuccess(AnotherResult<CaseinformationData> data) {
        ViseLog.d("查询病例列表"+data);
        caseAdapter.setNewData(data.getList());
    }

    @Override
    public void onSubmitSuccess(DataStateResult data) {

    }

    @Override
    public void onError(String msg) {

        if(msg!=null){
            msg(msg);
        }
    }

    @Override
    public void onRefreshUI() {
        caseAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDeleteCase(DataStateResult dataStateResult) {
        if(dataStateResult.getMsgCode().equals("0x00000000")){
            caseHistoryPresenter.GetPatientCaseHistory(setStatetime(ConstantManger.Aweek), format.format(new Date()));
        }
    }

    @Override
    public void onUpdateCase(DataStateResult dataStateResult) {

    }

    @Override
    public void DialogDetermine(String statsTime, String endTime) {
        caseHistoryPresenter.GetPatientCaseHistory(statsTime, endTime);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //刷新界面
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UpdateEvent updateEvent) {
        if (updateEvent.type.equals(ConstantManger.CASELIST)) {
            caseHistoryPresenter.GetPatientCaseHistory(setStatetime(ConstantManger.Aweek), format.format(new Date()));
            imageView4.setText("一周");
        }
    }
}
